#pragma once
#include "Connection.hpp"
#include "Protocol.hpp"
#include "cal.hpp"

class PackageParse
{
public:
    void Parse(Connection *conn)
    {
        // std::cout << conn->Inbuffer() << std::endl;
        Cal cal;
        std::string message;
        while ((message = DeCode(conn->Inbuffer())) != "")
        {
            std::cout << "recv request 序列化 : " << message << std::endl;
            // 数据不是完整的，继续读取
            // if (message == "")
            //     continue;
            // 3.读取到完整的序列化的数据，进行反序列化得到数据
            Request req;
            req.DeSerialize(message);

            // 4. 对数据进行操作得到结果
            //  调用处理函数
            // Response res = _func(req);
            Response res = cal.Excute(req);

            // 5.序列化response
            std::string send_message;
            res.Serialize(send_message);
            std::cout << "send response 序列化 : " << send_message << std::endl;

            // 6.为序列化数据添加报头
            send_message = EnCode(send_message);
            std::cout << "send response 报文 : " << send_message;

            // 7.发送数据给client
            //socket->Send(send_message);
            conn->AppendOutbuff(send_message);
        }

        if(conn->OutBuffer().size() && conn->_sender != nullptr)
        {
            conn->_sender(conn);
            // conn->_R->EnableReadWrite(conn->GetSockfd(), true, true);
        }
    }
};